import os

import requests
from lxml import etree

if __name__ == "__main__":
    # ua伪装
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62"
    }
    url = "https://pic.netbian.com/4kmeinv"
    response = requests.get(url, headers=headers)
    # response.encoding = "utf-8"
    tree = etree.HTML(response.text)
    liList = tree.xpath("//div[@class='slist']/ul/li")
    if not os.path.exists("./picLibs"):
        os.mkdir("./picLibs")
    for li in liList:
        imgSrc = "https://pic.netbian.com" + li.xpath("./a/img/@src")[0]
        # imgName = li.xpath("./a/b/text()")[0] + ".jpg"
        imgName = li.xpath("./a/img/@alt")[0] + ".jpg"
        imgName = imgName.encode('iso-8859-1').decode('gbk')
        # print(imgSrc, imgName)
        with open("./picLibs/" + imgName, "wb") as fp:
            fp.write(requests.get(imgSrc, headers=headers).content)
            print(imgName, "下载成功！！！")
